package com.woniushop.service.imple;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.woniushop.dao.OrderFormDAO;
import com.woniushop.dao.AddressDAO;
import com.woniushop.dao.imple.AddressDAOImple;
import com.woniushop.dao.imple.OrderFormDAOImple;
import com.woniushop.entity.Address;
import com.woniushop.entity.OrderForm;
import com.woniushop.entity.PageBean;
import com.woniushop.service.OrderFormService;

/**
 * 订单业务逻辑接口的实现类
 * 
 * @author 崔新雄
 *
 */
public class OrderFormServiceImple implements OrderFormService {

	private OrderFormDAO orderFormDAO = new OrderFormDAOImple();
	private AddressDAO addressDAO = new AddressDAOImple();

	/**
	 * 为用户创建一个购物车订单
	 * 
	 * @return
	 */
	@Override
	public String creatCart(Integer uid) {
		OrderForm orderForm = new OrderForm();
		orderForm.setUid(uid);
		orderForm.setOdate(new Date());
		orderForm.setOstate(1);
		orderForm.setAid(1);// 所有购物车订单中的地址都设为1

		// 生成唯一的购物车订单号
		String ono = UUID.randomUUID().toString().replaceAll("-", "");
		orderForm.setOno(ono);

		// 将订单数据写入数据库
		orderFormDAO.add(orderForm);
		return ono;
	}

	/**
	 * 根据订单号返回订单id
	 * 
	 * @param ono
	 * @return
	 */
	@Override
	public Integer getOidByOno(String ono) {
		OrderForm orderForm = orderFormDAO.findOne(ono);
		if (orderForm == null) {
			return null;
		}
		return orderForm.getOid();
	}

	/**
	 * 根据用户id生成一个已确认未支付的订单，并返回
	 * 
	 * @param uid
	 * @return
	 */
	@Override
	public OrderForm addNewOrder(Integer uid) {
		OrderForm orderForm = new OrderForm();
		orderForm.setUid(uid);
		orderForm.setOdate(new Date());
		orderForm.setOstate(2);
		Address address = addressDAO.findDefault(uid);
		orderForm.setAid(address.getAid());

		// 生成唯一的购物车订单号
		String ono = UUID.randomUUID().toString().replaceAll("-", "");
		orderForm.setOno(ono);
		orderFormDAO.add(orderForm);
		return orderFormDAO.findOne(ono);
	}

	@Override
	public void del(Integer oid) {
		orderFormDAO.delete(oid);
	}

	@Override
	public List<OrderForm> findValidByPage(PageBean pageBean, OrderForm orderForm) {
		// 完善pageBean信息
		Integer totalNum = orderFormDAO.count(orderForm);
		pageBean.setTotalNum(totalNum);

		Integer pageSize = pageBean.getPageSize();
		Integer totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
		totalPage = totalPage == 0 ? 1 : totalPage;
		pageBean.setTotalPage(totalPage);

		// 如果页码超过范围，则设为最后一页
		if (pageBean.getPage() > totalPage) {
			pageBean.setPage(totalPage);
		}

		// 查询地址数据
		List<OrderForm> list = orderFormDAO.findByPage(pageBean.getPage(), pageSize, orderForm);

		pageBean.setNumsOfCurrPage(list.size());

		return list;
	}

	/**
	 * 根据订单id返回订单
	 * 
	 * @param oid
	 * @return
	 */
	@Override
	public OrderForm findOne(Integer oid) {
		return orderFormDAO.findOne(oid);
	}

	/**
	 * 确认收货
	 * 
	 * @param oid
	 */
	@Override
	public void receipt(Integer oid) {
		OrderForm orderForm = orderFormDAO.findOne(oid);
		if (orderForm != null && orderForm.getOstate() == 4) {
			orderForm.setOstate(5);
			orderFormDAO.update(orderForm);
		}
	}

	/**
	 * 支付
	 * 
	 * @param oid
	 */
	@Override
	public void pay(OrderForm orderForm) {
		if (orderForm != null && orderForm.getOstate() == 2) {
			orderForm.setOstate(3);
			orderFormDAO.update(orderForm);
		}
	}

	/**
	 * 后台发货
	 * 
	 * @param oid
	 */
	@Override
	public void deliver(Integer oid) {
		OrderForm orderForm = orderFormDAO.findOne(oid);
		if (orderForm != null && orderForm.getOstate() == 3) {
			orderForm.setOstate(4);
			orderFormDAO.update(orderForm);
		}		
	}
}
